package be.gnx.fogo.application.ui;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import be.gnx.fogo.application.model.Address;
import be.gnx.fogo.application.model.Communication;
import be.gnx.fogo.application.model.Person;

public class PersonGroup extends AbstractParentGroup {

  private Text addressText;
  private Text telHomeText;
  private Text telWorkText;
  private Text telMobileText;
  private Text emailHomeText;
  private Text emailWorkText;

  private Button editButton;

  public PersonGroup(Composite composite, int style, final ResourceBundle resourceBundle) {
    super(composite, style, resourceBundle);
    this.setText("Person");
    addressText = new Text(getGroup(), SWT.NONE);
    telHomeText = new Text(getGroup(), SWT.NONE);
    telWorkText = new Text(getGroup(), SWT.NONE);
    telMobileText = new Text(getGroup(), SWT.NONE);
    emailHomeText = new Text(getGroup(), SWT.NONE);
    emailWorkText = new Text(getGroup(), SWT.NONE);

    editButton = new Button(getGroup(), SWT.PUSH);
    editButton.setText("edit");
    editButton.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent selectionEvent) {
        widgetSelected(selectionEvent);
      }
      public void widgetSelected(SelectionEvent selectionEvent) {
        Shell shell = new Shell(getDisplay(), SWT.NONE);
        PersonFullDetail personEdit = new PersonFullDetail(shell, resourceBundle);
        personEdit.setPerson(person);
        shell.open();
        while (!shell.isDisposed()) {
          if (!getDisplay().readAndDispatch()) {
            getDisplay().sleep();
          }
        }
        // TODO Auto-generated method stub
        
      };
    });
    resize();
  }

  protected void widgetDisposed(DisposeEvent disposeEvent) {
    // TODO;
  }

  protected void controlResized(ControlEvent controlEvent) {
    resize();
  }

  @Override
  protected void resize() {
    super.resize();
    int margin = 2;
    Point editButtonSize = editButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    editButton.setBounds(getGroup().getBounds().width - (editButtonSize.x + margin), getGroup().getBounds().height - (editButtonSize.y + margin), editButtonSize.x, editButtonSize.y);
  }

  protected void setAsFamily(boolean asFamily) {
    super.setAsFamily(asFamily);
    if (asFamily) {
      this.setText("Paternal");
    } else {
      this.setText("Person");
    }
  }

  protected void setPerson(Person person) {
    super.setPerson(person);
    if (person != null) {
      Address address = null;
      String phoneHome = "";
      String phoneWork = "";
      String phoneMobile = "";
      String phoneFax = "";
      String emailHome = "";
      String emailWork = "";
      if ((person.getCommunicationSet() != null) && (person.getCommunicationSet().size() > 0)) {
        for (Communication communication : person.getCommunicationSet()) {
          if (communication.getCategory().getName().equals("Primary Personal Address")) {
            address = communication.getAddress();
          }
          if (communication.getCategory().getName().equals("Primary Personal E-Mail")) {
            emailHome = communication.getString() + emailHome;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal E-Mail")) {
            emailHome += " " + communication.getString();
          }
          if (communication.getCategory().getName().equals("Primary Business E-Mail")) {
            emailWork = communication.getString() + emailWork;
          }
          if (communication.getCategory().getName().equals("Auxiliary Business E-Mail")) {
            emailWork += " " + communication.getString();
          }
          if (communication.getCategory().getName().equals("Primary Personal Phone Number")) {
            phoneHome = communication.getNumber() + phoneHome;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal Phone Number")) {
            phoneHome += " " + communication.getNumber();
          }
          if (communication.getCategory().getName().equals("Primary Business Phone Number")) {
            phoneWork = communication.getNumber() + phoneWork;
          }
          if (communication.getCategory().getName().equals("Auxiliary Business Phone Number")) {
            phoneWork += " " + communication.getNumber();
          }
          if (communication.getCategory().getName().equals("Primary Personal Mobile Number")) {
            phoneMobile = communication.getNumber() + phoneMobile;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal Mobile Number")) {
            phoneMobile += " " + communication.getNumber();
          }
          if (communication.getCategory().getName().equals("Primary Personal Fax Number")) {
            phoneFax = communication.getNumber() + phoneFax;
          }
          if (communication.getCategory().getName().equals("Auxiliary Personal Fax Number")) {
            phoneFax += " " + communication.getNumber();
          }
        }
      }

      addressText.setText(address.getStreetNumberBus() + "," + address.getPostcodeCity() + "," + address.getCountry().getIso_3166_1_alpha2_as_string());
      if (phoneHome != null) {
        telHomeText.setText(phoneHome);
      }
      if (phoneWork != null) {
        telWorkText.setText(phoneWork);
      }
      if (phoneMobile != null) {
        telMobileText.setText(phoneMobile);
      }
      if (emailHome != null) {
        emailHomeText.setText(emailHome);
      }
      if (emailWork != null) {
        emailWorkText.setText(emailWork);
      }
    }
  }

}
